﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RFW.Weather.WeatherFlash
{
	public class WFlash : Communication
	{
		private CurrentConditions _currentConditions = null;

        public WFlash(CurrentConditions currentConditions, string URI) 
            : base(URI, false)
        {
			_currentConditions = currentConditions;
        }

        public override void Process(System.Xml.Linq.XDocument results)
        {
        }

		public override void Process(string results)
		{
			try
			{
				string[] values = results.Split(',');

				_currentConditions.TimeStamp = new DateTime(1900, 1, 1).AddSeconds(Convert.ToInt64(values[0].Substring(values[0].IndexOf("F=") + 2)));
				_currentConditions.TimeOfUpdate = Convert.ToDateTime(values[1].URLDecode());

				_currentConditions.Wind = new Wind
				{
					DirectionDescription = values[2],
					Direction = values[3].ToDecimal(),
					DirectionRate = values[31].ToDecimal(),
					Speed = values[4].ToDecimal(),
					SpeedRate = values[32].ToDecimal(),
					Gust = values[5].ToDecimal(),
					GustRate = values[33].ToDecimal()
				};

				_currentConditions.InHumidity = new Humidity
				{
					Percentage = values[6].ToDecimal(),
					Rate = values[34].ToDecimal()
				};

				_currentConditions.Humidity = new Humidity
				{
					Percentage = values[7].ToDecimal(),
					Rate = values[35].ToDecimal()
				};

				_currentConditions.InTemperature = new Temperature
				{
					Degrees = values[8].ToDecimal(),
					Rate = values[36].ToDecimal(),
					HeatIndex = values[22].ToDecimal(),
					HeatIndexRate = values[50].ToDecimal()
				};

				_currentConditions.Temperature = new Temperature
				{
					Degrees = values[9].ToDecimal(),
					Rate = values[37].ToDecimal(),
					HeatIndex = values[23].ToDecimal(),
					HeatIndexRate = values[51].ToDecimal(),
					Dewpoint = values[24].ToDecimal(),
					DewpointRate = values[52].ToDecimal(),
					WindChill = values[21].ToDecimal(),
					WindChillRate = values[49].ToDecimal(),
					Evaportranspiration = values[18].ToDecimal(),
					EvaportranspirationRate = values[46].ToDecimal()
				};

				_currentConditions.Rain = new Rain
				{
					YearlyTotal = values[11].ToDecimal()
				};

				_currentConditions.Barometer = new Barometer
				{
					Raw = values[10].ToDecimal(),
					RawRate = values[38].ToDecimal(),
					SL = values[25].ToDecimal(),
					SLRate = values[53].ToDecimal()
				};

				_currentConditions.Solar = new Solar
				{
					UVIndex = values[19].ToDecimal(),
					UVIndexRate = values[47].ToDecimal(),
					Radiation = values[20].ToDecimal(),
					RadiationRate = values[48].ToDecimal()
				};


				_currentConditions.PressureAltitude = values[26].ToDecimal();
				_currentConditions.CloudBase = values[27].ToDecimal();
				_currentConditions.DensityAltitude = values[28].ToDecimal();
				_currentConditions.VirtualTemperature = values[29].ToDecimal();
				_currentConditions.VaporPressure = values[30].ToDecimal();

			}
			catch (Exception ex)
			{
				System.Diagnostics.Debug.WriteLine(ex.Message);
			}
		}

        internal override void Failed()
        {
        }
    }
}
